(function(angular) {
	'use strict';
	angular.module("fs.waf").factory("addressInfoService", addressInfoService);
	addressInfoService.$inject = [ '$q', 'loggerFactory', 'ajaxServiceFactory' ];
	function addressInfoService($q, loggerFactory, ajaxServiceFactory) {
		var log = loggerFactory.getLogger("addressInfoService"), ajaxService = ajaxServiceFactory.buildAjaxService({
			getCountry: {
				url : 'do/cda/dataList/country',
				method : 'get'
			},
			stateAll: {
				url : 'do/cda/dataList/stateAll',
				method : 'get'
			},
			getCitiesAll : {
				url : 'do/cda/dataList/citiesAll',
				method : 'get'
			}
		});
		
		function initDataList(){
			var defer1 = $q.defer(), defer2 = $q.defer(), defer3 = $q.defer();
			
			ajaxService.getCountry().then(function(response){
				var countryList = response ? response.data : [];
				defer1.resolve(countryList);
			});
			ajaxService.stateAll().then(function(response){
				var stateList = response ? response.data : [];
				defer2.resolve(stateList);
			});
			ajaxService.getCitiesAll().then(function(response){
				var cityList = response ? response.data : [];
				defer3.resolve(cityList);
			});
			
			return $q.all({
				countryList : defer1.promise,
				stateList : defer2.promise,
				cityList : defer3.promise
			});
		}
		
		function getStateOptions(stateList, countryCode){
			var result = [];
			if(stateList && stateList.length > 0 && countryCode){
				angular.forEach(stateList, function(data, index, array){
					if(data.countryCode == countryCode){
						result.push({
							"value" : data.stateCode,
							"label" : data.stateCode
						});
					}
				});
			}
			return result;
		}
		
		function getCityOptions(cityList, countryCode, stateCode){
			var result = [];
			if(cityList && cityList.length > 0 && countryCode && stateCode){
				angular.forEach(cityList, function(data, index, array){
					if(data.countryCode == countryCode && data.stateCode == stateCode){
						result.push({
							"value" : data.cityCode,
							"label" : data.cityCode
						});
					}
				});
			}
			return result;
		}
		
		function getStateOptionAll(stateList){
			var result = [];
			if(stateList && stateList.length > 0){
				angular.forEach(stateList, function(data, index, array){
					result.push({
						"value" : data.stateCode,
						"label" : data.stateCode
					});
				});
			}
			return result;
		}
		
		function getCityOptionAll(cityList){
			var result = [];
			if(cityList && cityList.length > 0){
				angular.forEach(cityList, function(data, index, array){
					result.push({
						"value" : data.cityCode,
						"label" : data.cityCode
					});
				});
			}
			return result;
		}
		
		function getCountryOptionAll(countryList){
			var result = [];
			if(countryList && countryList.length > 0){
				angular.forEach(countryList, function(data, index, array){
					result.push({
						"value" : data.value,
						"label" : data.value
					});
				});
			}
			return result;
		}
		
		function initData(){
			return $q(function(resolved, rejected){
				var result = {};
				initDataList().then(function(response){
					result.countryOptionAll = getCountryOptionAll(response.countryList);
					result.stateOptionAll = getStateOptionAll(response.stateList);
					result.cityOptionAll = getCityOptionAll(response.cityList);
					result.countryOptions = response.countryList;
					result.stateList = response.stateList;
					result.cityList = response.cityList;
					resolved(result);
				});
			});
		}
	
		
		return {
			initData : initData,
			getStateOptions : getStateOptions,
			getCityOptions : getCityOptions
		};
	}
})(angular);